package com.jcn.seckill.rabbitmq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jcn.seckill.redis.RedisService;

//将自动注册到Spring容器，不需要再在applicationContext.xml文件定义bean了
@Service
//发送者
public class MQSender {

	private static Logger log = LoggerFactory.getLogger(MQSender.class);
	
	//对类成员变量、方法及构造函数进行标注，完成自动装配的工作
	//@Autowired标注可以放在成员变量上，也可以放在成员变量的set方法上，也可以放在任意方法上表示
	@Autowired
	//注入接口
	AmqpTemplate amqpTemplate ;
	
	public void sendMiaoshaMessage(MiaoshaMessage mm) {
		String msg = RedisService.beanToString(mm);
		log.info("send message:"+msg);
		//将消息放入MIAOSHA_QUEUE中
		amqpTemplate.convertAndSend(MQConfig.MIAOSHA_QUEUE, msg);
	}
	//1.首先有一个队列和交换机，再进行绑定，绑定需要指定header
	//2.发送需要原始信息+头部信息
	//3.接收找到queue接收出来
//	public void send(Object message) {
	    //将对象转化成字符串
//		String msg = RedisService.beanToString(message);
		//打印
//		log.info("send message:"+msg);
//		amqpTemplate.convertAndSend(MQConfig.QUEUE, msg);
//	}
//	
//	public void sendTopic(Object message) {
	    //将对象转化成字符串
//		String msg = RedisService.beanToString(message);
		//打印
//		log.info("send topic message:"+msg);
//		amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE, "topic.key1", msg+"1");
//		amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE, "topic.key2", msg+"2");
//	}
//	
//	public void sendFanout(Object message) {
		//将对象转化成字符串
//		String msg = RedisService.beanToString(message);
		//打印
//		log.info("send fanout message:"+msg);
//		amqpTemplate.convertAndSend(MQConfig.FANOUT_EXCHANGE, "", msg);
//	}
//	
//	public void sendHeader(Object message) {
		//将对象转化成字符串
//		String msg = RedisService.beanToString(message);
		//打印
//		log.info("send fanout message:"+msg);
//		MessageProperties properties = new MessageProperties();
		//值要和headerBinding中的key,value一样，才能放进去
//		properties.setHeader("header1", "value1");
//		properties.setHeader("header2", "value2");
//		Message obj = new Message(msg.getBytes(), properties);
//		amqpTemplate.convertAndSend(MQConfig.HEADERS_EXCHANGE, "", obj);
//	}

	
	
}
